// 获取本部门及以下id
export function getIdLists(arr, ids = [], id = 'id') {
  for (let i = 0; i < arr.length; i++) {
    ids.push(arr[i][id]);
    if (arr[i].children.length) {
      getIdLists(arr[i].children, ids, id);
    }
  }
  ids.map((id) => parseInt(id));
  return ids;
}

// 返回部门权限条件 用户表的查询
export function getDeptWhere(session, where: any = {}) {
  const dataScopeList = session.userInfo.roles.map((item) =>
    parseInt(item.data_scope),
  );
  const data_scope = Math.max(...dataScopeList); // 取最大值
  if (data_scope === 0) {
    return where;
  } else if (data_scope === 1) {
    // 部门及以下权限
    where.dept = getIdLists(
      session.departmentsObj.obj[session.userInfo.dept.id].children,
      [session.userInfo.dept.id],
      'id',
    );
  } else if (data_scope === 2) {
    // 部门权限
    where.dept = [session.userInfo.dept.id];
  } else {
    // 本人权限
    where.id = session.userInfo.id;
  }
  return where;
}
